package ext.acae2.change.autonumber;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;

import wt.change2.ChangeHelper2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.enterprise.RevisionControlled;
import wt.enterprise.SequenceGenerator;
import wt.fc.Persistable;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.VersionControlHelper;

import com.ptc.core.meta.common.TypeIdentifierHelper;

import ext.acae2.ACAEConstants;

/**
 * @desription CN number auto generator
 * @author cnie
 * @date 2013-5-10 下午4:23:46
 */
public class ECNNumberGenerator {

	/**
	 * 
	 * @description get ECN generate Number
	 * @author cnie
	 * @date 2013-5-12 下午4:24:53
	 * @version 1.0
	 * @param p
	 * @return
	 * @throws WTException
	 */
	public static String getECNNumber(Persistable p) throws WTException {
		StringBuffer ecnNumberSb = new StringBuffer();
		if (p instanceof RevisionControlled) {
			RevisionControlled rc = (RevisionControlled) p;
			String pNum;
			if (rc instanceof WTPart) {
				WTPart part = (WTPart) rc;
				pNum = part.getNumber();
			} else {
				WTDocument doc = (WTDocument) rc;
				pNum = doc.getNumber();
			}
			QueryResult qr = VersionControlHelper.service.allIterationsOf(rc
					.getMaster());
			while (qr.hasMoreElements()) {
				RevisionControlled itRc = (RevisionControlled) qr.nextElement();
				QueryResult ecnQr = ChangeHelper2.service
						.getLatestUniqueAffectingChangeOrders(itRc);
				List<WTChangeOrder2> ecnList = new ArrayList<WTChangeOrder2>();
				if(ecnQr.size()>0){
					Vector<WTChangeOrder2> ecnVc = ecnQr.getObjectVectorIfc().getVector();
					ecnList.addAll(ecnVc);
					Collections.sort(ecnList, new Comparator<WTChangeOrder2>() {
						@Override
						public int compare(WTChangeOrder2 ecn1, WTChangeOrder2 ecn2) {
							return ecn2.getNumber().compareTo(ecn1.getNumber());
						}
					});
				}
				for (WTChangeOrder2 ecn:ecnList) {
					String typeIdentifierStr = TypeIdentifierHelper.getType(ecn).toExternalForm();
					if (ecn.getNumber().indexOf(pNum) > -1
							&&( typeIdentifierStr
									.indexOf(ACAEConstants.ACAE2_TYPE_FILE_ECN) > -1
							|| typeIdentifierStr
									.indexOf(ACAEConstants.ACAE2_TYPE_PARTDRAWING_ECN) > -1)) {
						String currentECNNum = ecn.getNumber();
						int index = currentECNNum
								.lastIndexOf(ACAEConstants.ACAE2_ECN_NUMBER_DEFAULT_CODE) + 2;
						String prefixStr = currentECNNum.substring(0, index);
						String liushuihao = currentECNNum.substring(index);
						int nextSeq = Integer.parseInt(liushuihao) + 1;
						String nextLiushuihao = String.valueOf(nextSeq);
						if (nextLiushuihao.length() < 4) {
							ecnNumberSb.append(prefixStr);
							for (int k = 0; k < 4 - nextLiushuihao.length(); k++) {
								ecnNumberSb.append("0");
							}
							ecnNumberSb.append(nextLiushuihao);
						}
						break;
					}
				}
			}
			if (StringUtils.isBlank(ecnNumberSb.toString())) {
				ecnNumberSb.append(pNum)
						.append(ACAEConstants.ACAE2_ECN_NUMBER_DEFAULT_CODE)
						.append("0001");
			}
		}

		if (StringUtils.isBlank(ecnNumberSb.toString())) {
			String genStr = SequenceGenerator
					.generateValue("WTCHANGEORDERID_seq");
			if (genStr.length() < 5) {
				for (int k = 0; k < 5 - genStr.length(); k++) {
					ecnNumberSb.append("0");
				}
				ecnNumberSb.append(genStr);
			}
		}
		return ecnNumberSb.toString();
	}

}
